package ph.edu.upd.eee.upcatarchivesearch;

import android.app.Activity;
import android.app.DialogFragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    EditText editTxtQuery;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editTxtQuery = (EditText) findViewById(R.id.edittxt_main_search);

        if(!Utility.archiveCheck(this)) {
            Toast.makeText(getApplicationContext(), "No archives found. Update now!", Toast.LENGTH_LONG).show();
        }

        editTxtQuery.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_NULL && keyEvent != null && keyEvent.getAction() == KeyEvent.ACTION_DOWN){
                    // Triggered by enter key
                    startSearchActivity();
                }

                return true;
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        switch(id) {
            case R.id.menu_settings:
                return true;
            case R.id.menu_update:
                if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                    DialogFragment dFrag = UpdateProgressDialogFragment.newInstance();
                    dFrag.setCancelable(false);
                    dFrag.show(getFragmentManager(), "updateDialog");
                }
                else {
                    Toast.makeText(this, R.string.toast_nosdcard, Toast.LENGTH_LONG).show();
                }

                return true;
            case R.id.menu_about:
                startActivity(new Intent(this, AboutActivity.class));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void startSearchActivity() {
        if(!Utility.archiveCheck(this)) {
            // No archives to search or corrupt archives
            Toast.makeText(getApplicationContext(), R.string.toast_nofiles, Toast.LENGTH_LONG).show();
        }
        else if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            // External storage not available
            Toast.makeText(getApplicationContext(), R.string.toast_nosdcard, Toast.LENGTH_LONG).show();
        } else if (SearchArchive.getTermType(editTxtQuery.getText().toString()) == null) {
            // Invalid search terms
            Toast.makeText(getApplicationContext(), R.string.toast_invalidterm, Toast.LENGTH_LONG).show();
        } else {
            Intent intent = new Intent(getApplicationContext(), SearchResultsActivity.class);
            intent.putExtra(SearchResultsActivity.EXTRA_SEARCH_TERM, editTxtQuery.getText().toString());

            startActivity(intent);
        }
    }
}
